package com.jww.service.impl;

import com.jww.domain.User;
import com.jww.mapper.UserMapper;
import com.jww.service.UserService;
import com.jww.utils.MyUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {
    @Override
    public List<User> selectAll() {
        SqlSession sqlSession = MyUtils.getSqlSession();
        //得到动态代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.selectAll();
        MyUtils.close(sqlSession);
        return users;
    }

    @Override
    public void update(User user) {
        SqlSession session = MyUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.update(user);
        MyUtils.close(session);

    }
    @Override
    public void save(User user) {
        SqlSession session = MyUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.save(user);
        MyUtils.close(session);
    }

    @Override
    public void deleteByIds(String ids) {
        SqlSession session = MyUtils.getSqlSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        Integer[] idInt = null;

        if (ids != null && ids.length() > 0) {
            //处理ids字符串:将字符串变成整型数组
            String[] array = ids.split(",");
            idInt = new Integer[array.length];
            for (int i = 0; i < array.length; i++) {
                idInt[i] = Integer.parseInt(array[i]);
            }
        }
        mapper.deleteByIds(idInt);
        MyUtils.close(session);
    }


}
